import '../css/Login.css';
import React, { Component } from 'react'

import { login as doLogin} from '../api'


class Login extends Component {
    async login(e){
        e.preventDefault()
        const fd = new FormData(e.target)

        const res = await doLogin(fd.get('username'), fd.get('password'))
        window.sessionStorage.setItem('authkey', res.data.data.authKey)
        window.sessionStorage.setItem('sessionid', res.data.data.sessionId)
        console.log(res)
        if(res.data.code == 200){
            this.props.history.replace('/')
        }else{
            alert(res.data.error)
        }
        
        
    }
    render(){
        return (
            <div className="box">
            <div className="left">
			    <img src="http://crm.cimns.com/static/img/logo.3e34073.png" alt="" className="logo"></img>
		    </div>
            <div className="right">
                <div className="register">
                    <div>
                        <p>悟空CRM</p>
                    </div>
                    <form onSubmit={this.login.bind(this)}>
                        <div>
                            <input className="account" name="username" type="username" placeholder="请输入用户名"/>
                        </div>
                        <div className="hint"></div>
                        <div>
                            <input className="password" name="password" type="password"  placeholder="请输入密码"/>
                        </div>
                        <div  className="hint"></div>
                        <div>
                            <button className="login_button">登录</button>
                        </div>
				    </form>
                </div>
                <div>
                    <p></p>
                </div>
            </div>
        </div>
        )
    }
}

export default Login